package controller

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"kf/models/dao"
	"kf/models/entity"
	"kf/utils/R"
	"net/http"
)

type SysAutoReplyController struct {
	SysAutoReplyDao *dao.SysAutoReplyDao
	Log             *zap.Logger
}

// 分页获取角色
func (sysAutoReplyController *SysAutoReplyController) GetAutoReplyPage(context *gin.Context) {
	var page entity.AutoReplyPageQuery
	err := context.ShouldBindQuery(&page)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	replyPage, err := sysAutoReplyController.SysAutoReplyDao.GetSysAutoReplyPage(page)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	count, err := sysAutoReplyController.SysAutoReplyDao.GetSysAutoReplyCount(page.Content)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"list":  replyPage,
			"count": count,
		},
	})
}

// 创建或者修改
func (sysAutoReplyController *SysAutoReplyController) SaveOrUpdateAutoReply(context *gin.Context) {
	var autoReply entity.SysAutoReply
	err := context.ShouldBindJSON(&autoReply)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	if autoReply.ID != 0 {
		err = sysAutoReplyController.SysAutoReplyDao.UpdateAutoReply(autoReply)
	} else {
		err = sysAutoReplyController.SysAutoReplyDao.SaveAutoReply(autoReply)
	}

	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(true))
}

// 根据id删除
func (sysAutoReplyController *SysAutoReplyController) DeleteAutoReply(context *gin.Context) {
	idStr := context.Param("id")
	b, err := sysAutoReplyController.SysAutoReplyDao.DeleteAutoReply(idStr)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg(err.Error()))
		return
	}
	context.JSON(http.StatusOK, R.ReturnSuccess(b))
}
